# 数据库操作

import pymysql

from DBUtils.PooledDB import PooledDB

from config.config import configs

connConfig = configs()

pool = PooledDB(
    creator=pymysql,
    mincached=2,
    maxcached=5,
    maxshared=0,
    maxconnections=15,
    blocking=True,
    maxusage=None,
    setsession=None,
    autocommit = True,
    host=connConfig.connHost,
    port=connConfig.connPort,
    user="THole",
    password=connConfig.connPwd,
    db="thole",
    charset="utf8"
    )

conn = pool.connection()

# 数据库类
class dataBase():
    def __init__(self):
        super().__init__()

    class index():
        def __init__(self):
                super().__init__()
                self.cursor = conn.cursor()

                self.sql_cardLen = "SELECT index_card.ID FROM index_card"

                self.sql_cardNew = "SELECT * FROM index_card where ID BETWEEN %s AND %s"
                self.sql_cardLen = "SELECT index_card.ID FROM index_card"
                self.sql_cardLen = "SELECT index_card.ID FROM index_card"

                self.cardLen = self.cursor.execute(self.sql_cardLen)

        def get(self, lastID:int, device:int):
            # PC端：一次16张    'device':0_'PC'
            # PE端：一次8张     'device':1_'PE'
            needNum = 1
            if (device == 1):
                needNum = lastID + 8
            args = (needNum, lastID)
            self.cursor.execute(self.sql_cardNew, args)
            return self.cursor.fetchall()